package cn.walls1717.wallsmall.search.controller;

import cn.walls1717.wallsmall.common.pojo.to.es.SkuEsModel;
import cn.walls1717.wallsmall.common.utils.R;
import cn.walls1717.wallsmall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

import static cn.walls1717.wallsmall.common.exception.BizCodeEnum.PRODUCT_UP_EXCEPTION;

/**
 * 保存数据
 *
 * @author walls1717
 * @since 2022/11/02 14:45
 **/
@Slf4j
@RestController
@RequestMapping("/search/save")
public class EsSaveController {

    @Resource
    private ProductSaveService productSaveService;

    /**
     * 商品上架
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> skuEsModels) {
        boolean flag = false;
        try {
            flag = productSaveService.productStatusUp(skuEsModels);
        } catch (IOException e) {
            log.error("商品上架异常 -> {}", e.toString());
            return R.error(PRODUCT_UP_EXCEPTION.getCode(), PRODUCT_UP_EXCEPTION.getMsg());
        }
        return flag ? R.ok() : R.error(PRODUCT_UP_EXCEPTION.getCode(), PRODUCT_UP_EXCEPTION.getMsg());
    }
}
